/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2017-07-21 11:42:21
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
package com.itttq.gshop.pbpm.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.itttq.gshop.domain.po.ErpOrderPo;
import com.itttq.gshop.domain.to.ErpOrderTo;
import com.itttq.gshop.domain.to.ResultPageTo;
import com.itttq.gshop.domain.to.ResultTo;
import com.itttq.gshop.generic.controller.GenericController;
import com.itttq.gshop.pbpm.service.ErpOrderService;

@RestController
@RequestMapping("erpOrder")
public class ErpOrderController extends GenericController {
	private static final long serialVersionUID = -6746968109152121L;
	public static final String CACHE_KEY = "erpErpOrder";
	@Autowired
	private ErpOrderService orderService;
	
	/**
	 * 根据条件获取ErpOrderPo对象，并返回查询的数据总数
	 * 
	 * @param erpOrder
	 * @Author 迷蝶
	 * @return ResultPageTo<ErpOrderPo>
	 */
	@RequestMapping("list")
	public ResultPageTo<ErpOrderPo> getErpOrders(@RequestBody ErpOrderPo erpOrder) {
		return orderService.getErpOrders(erpOrder);
	}
	
	/**
	 * 根据条件获取ErpOrderPo对象，并返回查询的数据总数
	 * 
	 * @param erpOrder
	 * @Author 迷蝶
	 * @return List<OrderTo>
	 */
	@RequestMapping("orders")
    public List<ErpOrderTo> getOrderDetails(@RequestBody ErpOrderPo erpOrder) {
        return orderService.getOrders(erpOrder);
    }
	
	/**
	 * 根据ID获取ErpOrderPo对象，并放入redis缓存
	 * 
	 * @param id	只能是数字
	 * @Author 迷蝶
	 * @return ErpOrderPo
	 */
	@RequestMapping("{id:^[0-9]*$}")
	@Cacheable(value=CACHE_KEY, key="'erpOrder_'+#id")
	public ErpOrderPo getErpOrder(@PathVariable("id") int id) {
		return orderService.getErpOrder(id);
	}
	
	/**
	 * 保存ErpOrderPo对象，并清除此ID的缓存。<br>
	 * 如果ID存在，则更新对象；ID不存在则插入对象。
	 * 
	 * @param erpOrder
	 * @Author 迷蝶
	 * @return ErpOrderPo
	 */
	@RequestMapping("save")
	@CacheEvict(value=CACHE_KEY, key="'erpOrder_'+#erpOrder.id")
	public Integer saveErpOrder(@RequestBody ErpOrderTo order) {
		int rs = 0;
		if(null == order) {return rs;}
		if(null == order.getId()) {
			ResultTo rt = orderService.addErpOrder(order);
			rs = rt.getCode();
		} else {
			rs = orderService.updateErpOrder(order);
		}
		return rs;
	}
	
	/**
	 * 根据ID删除ErpOrderPo对象，并清除此ID的缓存。<br>
	 * 
	 * @param id	只能是数字
	 * @Author 迷蝶
	 * @return ErpOrderPo
	 */
	@RequestMapping("del/{id:^[0-9]*$}")
	@CacheEvict(value=CACHE_KEY, key="'erpOrder_'+#id")
	public Integer delErpOrder(@PathVariable("id") int id) {
		return orderService.delErpOrder(id);
	}
}